package alik.notes.server.persistence;

import java.io.Serializable;
import java.util.StringTokenizer;

public class ComposedIdKey implements Serializable {

	private static final String DELIM = "::";
	
	private static final long serialVersionUID = -6247300690220960474L;
	public String field1;
	public String field2;

	/**
	 * Default constructor.
	 */
	public ComposedIdKey() {
	}

	/**
	 * String constructor.
	 */
	public ComposedIdKey(String value) {
		
		StringTokenizer token = new StringTokenizer(value, DELIM);
		// className
		token.nextToken();
		// field1
		this.field1 = token.nextToken();
		// field2
		this.field2 = token.nextToken();
	}

	/**
	 * String constructor.
	 */
	public ComposedIdKey(String field1, String field2) {
		// field1
		this.field1 = field1;
		// field2
		this.field2 = field2;
	}
	
	/**
	 * Implementation of equals method.
	 */
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof ComposedIdKey)) {
			return false;
		}
		ComposedIdKey c = (ComposedIdKey) obj;

		return field1.equals(c.field1) && field2.equals(c.field2);
	}

	/**
	 * Implementation of hashCode method that supports the equals-hashCode
	 * contract.
	 */
	public int hashCode() {
		return this.field1.hashCode() ^ this.field2.hashCode();
	}

	/**
	 * Implementation of toString that outputs this object id's PK values.
	 */
	public String toString() {
		return this.getClass().getName() + DELIM + this.field1 + DELIM + this.field2;
	}
}